
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>MySQL 如何实现悲观锁和乐观锁？ - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>MySQL 如何实现悲观锁和乐观锁？ - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">悲观锁的核心思想或基本假设是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">悲观锁假设在多事务并发访问时很可能会发生数据冲突。因此，在操作数据之前，它会先对数据进行加锁，以防止其他事务同时进行修改。</div>
          </div>
          <div class="card-source">来源: 1. 悲观锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在数据库层面，实现悲观锁的两种常见SQL语句是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">悲观锁通常通过数据库的锁机制来实现，例如使用 `SELECT ... FOR UPDATE` 来加排他锁，或者使用 `LOCK IN SHARE MODE` 来加共享锁。</div>
          </div>
          <div class="card-source">来源: 1. 悲观锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">悲观锁的主要优点和缺点分别是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">优点是能够有效防止并发事务之间的冲突，保证数据一致性。缺点是由于锁定了资源，可能会增加其他事务的等待时间，从而降低系统的并发性能。</div>
          </div>
          <div class="card-source">来源: 1. 悲观锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">乐观锁的核心思想或基本假设是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">乐观锁假设数据冲突很少发生。它不会在操作前加锁，而是在提交数据更新时，检查在此期间是否有其他事务修改过数据。如果检测到冲突，则回滚操作并重新尝试。</div>
          </div>
          <div class="card-source">来源: 2. 乐观锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">乐观锁最常见的实现方式是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">乐观锁通常通过版本号（version）或时间戳机制来实现。常见的方式是给数据表增加一个`version`字段，每次更新数据时先检查`version`是否发生变化。</div>
          </div>
          <div class="card-source">来源: 2. 乐观锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">乐观锁的主要优点和缺点分别是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">优点是无需加锁，避免了锁带来的性能开销，适合读多写少的场景。缺点是在高并发的写场景下，可能导致频繁的冲突和重试，反而影响系统性能。</div>
          </div>
          <div class="card-source">来源: 2. 乐观锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">悲观锁更适用于哪种业务场景？请举例说明。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">悲观锁适用于写操作多、数据冲突概率大的场景，特别是需要强一致性保证的关键业务，例如银行账户转账。</div>
          </div>
          <div class="card-source">来源: 3. 应用场景与选择</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">乐观锁更适用于哪种业务场景？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">乐观锁适用于读操作多、数据冲突概率小的场景，例如大多数的业务查询操作。在写操作相对较少的情况下，乐观锁能有效减少资源锁定带来的性能问题。</div>
          </div>
          <div class="card-source">来源: 3. 应用场景与选择</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在使用悲观锁和乐观锁时，分别需要注意什么关键问题？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">使用悲观锁时，需要小心死锁问题，尤其是在多表更新或复杂查询场景下。使用乐观锁时，需要考虑设计合理的重试机制和用户体验，以应对更新失败的情况。</div>
          </div>
          <div class="card-source">来源: 4. 注意事项</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
